package com.ohy.tencent.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.ohy.tencent.config.Contan;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("tencent")
@Slf4j
public class TencentVideoController {

    @GetMapping(value = "getVideo")
    public String getVideo(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                           @RequestParam(value = "sort", required = false, defaultValue = "18") int sort,
                           @RequestParam(value = "itype", required = false, defaultValue = "-1") int itype,
                           @RequestParam(value = "iyear", required = false, defaultValue = "-1") int iyear,
                           @RequestParam(value = "iarea", required = false, defaultValue = "-1") int iarea,
                           @RequestParam(value = "iawards", required = false, defaultValue = "-1") int iawards) {
        String url = "https://list.video.qq.com/fcgi-bin/list_common_cgi?novalue=1&otype=json&platform=5&version=10000&tid=573&uappkey=d2a6457eb8ae927a&uappid=20001172&intfname=hollywood_movie&sourcetype=1&type=1&pagesize=21&offset=" + ((page - 1) * 21) + "" +
                "&sort=" + sort + "&itype=" + itype + "&iyear=" + iyear + "&iarea=" + iarea + "&iawards=" + iawards;
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body().substring("QZOutputJson=".length(), execute.body().length() - 1);
    }

    @GetMapping(value = "getMVClass")
    public String getClassify() {
        String url = "https://list.video.qq.com/fcgi-bin/list_select_cgi?platform=5&version=10000&otype=json&intfname=hollywood_movie";
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        String result = execute.body().substring("QZOutputJson=".length(), execute.body().length() - 1);
        return result;
    }

    @GetMapping(value = "getTVClass")
    public String getTVClass() {
        String url = "https://list.video.qq.com/fcgi-bin/list_select_cgi?platform=5&version=10000&otype=json&intfname=hollywood_tv";
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        String result = execute.body().substring("QZOutputJson=".length(), execute.body().length() - 1);
        return result;
    }

    @GetMapping(value = "getTVVideo")
    public String getTVVideo(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                             @RequestParam(value = "sort", required = false, defaultValue = "18") int sort,
                             @RequestParam(value = "itype", required = false, defaultValue = "-1") int itype,
                             @RequestParam(value = "iyear", required = false, defaultValue = "-1") int iyear,
                             @RequestParam(value = "iarea", required = false, defaultValue = "-1") int iarea) {

        String url = "https://list.video.qq.com/fcgi-bin/list_common_cgi?novalue=1&otype=json&platform=5&version=10000&tid=573&uappkey=d2a6457eb8ae927a&uappid=20001172&intfname=hollywood_tv&type=2&pagesize=21&offset=" + ((page - 1) * 21) + "" +
                "&sort=" + sort + "&itype=" + itype + "&iyear=" + iyear + "&iarea=" + iarea;
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body().substring("QZOutputJson=".length(), execute.body().length() - 1);
    }

    @GetMapping(value = "getTVInfo")
    public String getTVInfo(@RequestParam(value = "url") String url) {
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        String res = execute.body();
        if (!res.contains("var VIDEO_INFO = ")) return "";
        res = res.substring(res.indexOf("var COVER_INFO = ") + "var COVER_INFO = ".length());
        res = res.substring(0, res.indexOf("var COLUMN_INFO = {}"));
        return res;
    }

    @GetMapping(value = "getSearch")
    public String getSearch(@RequestParam(value = "word") String word) {
        String url = "https://s.video.qq.com/smt_wap?num=10&otype=json&query=" + word;
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body().substring("QZOutputJson=".length(), execute.body().length() - 1);
    }


    @GetMapping(value = "getSearchRes")
    public String getSearchRes(@RequestParam(value = "word") String word) throws UnsupportedEncodingException {
        String url = "https://m.v.qq.com/search.html?act=2&keyWord=" + URLEncoder.encode(word, "UTF-8");
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        String res = execute.body();
        Document parse = Jsoup.parse(res);
        Elements elements = parse.getElementsByClass("search_item");
        List<JSONObject> list = new ArrayList<>();
        for (Element element : elements) {
            try {
                if (element.getElementsByClass("figure_head").size() == 0) continue;
                if (element.getElementsByClass("figure_source _figure_src").size() > 0
                        && !element.getElementsByClass("figure_source _figure_src").get(0).text().contains("腾讯视频") &&
                        !element.getElementsByClass("figure_source _figure_src").get(0).text().contains("暂无")) continue;
                String score = element.getElementsByClass("mask_scroe").text();
                if (StringUtils.isEmpty(score)) continue;
                String title = element.getElementsByClass("figure_title").get(0).text();
                String pic = element.getElementsByClass("figure _figure").get(0).getElementsByTag("img").get(0).attr("src");
                url = element.getElementsByClass("figure _figure").get(0).getElementsByTag("a").get(0).attr("href");
                String type = element.getElementsByClass("episode_item").size() > 0 ? "tv" : "mv";
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("score", score);
                jsonObject.put("title", title);
                jsonObject.put("pic", pic);
                jsonObject.put("url", type.equals("tv") ? "https://v.qq.com/x/cover/" + url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")) + "/" + url.substring(url.lastIndexOf("=") + 1) + ".html" : url);
                jsonObject.put("type", type);
                list.add(jsonObject);
            } catch (Exception e) {
                log.error("解析腾讯视频搜索异常", e);
            }
        }
        return list.toString();
    }

}
